////////////////////////////////////////////////////////////////////////////////
//
//  FLEX USER GROUP HOKURIKU
//  Copyright 2009 Flex User Group Hokuriku
//  All Rights Reserved.
// 
//  NOTICE: Flex User Group Hokuriku permits you to use, 
//  modify, and distribute this file 
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package net.fxug.hokuriku.chocolat.helper
{
    import mx.formatters.Formatter;

    [ResourceBundle("formatters")]
    [ResourceBundle("SharedResources")]

    /**
     * タイマー用タイムフォーマットクラス
     * 
     * @author sho
     */
    public class TimeFormatter extends Formatter
    {
		private var _trim:Boolean = false;
		
        public function TimeFormatter(trim:Boolean = false)
        {
            super();
			this._trim = trim;
        }

		public function get trim():Boolean
		{
			return this._trim;
		}

		public function set trim(value:Boolean):void
		{
			this._trim = value;
		}

        override public function format(value:Object):String
        {
        	var timeString:String = "";
        	
            if (error)
                error = null;

            if (value === null || isNaN(Number(value)))
            {
                error = defaultInvalidValueError;
                return "";
            }
            
            var time:int = int(value) / 1000;
            var hours:int = time / 60 / 60;
            var minutes:int = time / 60 - hours * 60;
            var seconds:int = time - hours * 60 * 60 - minutes * 60;
        
			timeString += (!trim || hours > 0) ? hours + ":" : "";
			timeString += (!trim || hours > 0 || minutes > 0) ? zeroFill(minutes) + ":" : "";
			timeString += (!trim || hours > 0 || minutes > 0 || seconds > 0) ? zeroFill(seconds) : "";           

            return timeString;
        }
        
        private function zeroFill(value:int):String
        {
            if (value > 9)
            {
                return value.toString();
            }
            else
            {
                return "0" + value;
            }
        }
    }
}
